package demo.DataStrucAndAlgo.Easy_pritice;

import java.util.ArrayDeque;
import java.util.Queue;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] data = new int[n];
        node tree = new node();
        node temp = tree;
        tree.data = sc.nextInt();
        for (int i = 0; i < n; i++) {
            int value = sc.nextInt();

            temp = findroot(temp,value);
            temp.left = new node();
            temp.left.data = sc.nextInt();
            temp.right = new node();
            temp.right.data = sc.nextInt();
        }
        tree.preOrder();
        tree.infixOrder();
        tree.postOrder();
    }
    static node findroot(node root,int data){
        if(root.data == data){
            return root;
        }
        if(root.left!= null){
            return findroot(root.left,data);
        }else if(root.right != null){
            return findroot(root.right,data);
        }
        return null;
    }
}
class node{
    int data;
    node left;
    node right;
    public void preOrder(){
        System.out.println(this.data);
        if(this.left != null){
            this.left.preOrder();
        }
        if(this.right != null){
            this.right.preOrder();
        }
    }

    //中序遍历
    public void infixOrder(){
        if(this.left != null){
            this.left.infixOrder();
        }
        System.out.print(this.data);
        if(this.right != null){
            this.right.infixOrder();
        }
    }

    //后序遍历
    public void postOrder(){
        if(this.left != null){
            this.left.postOrder();
        }
        if(this.right != null){
            this.right.postOrder();
        }
        System.out.print(this.data);
    }
}